package cn.stronglink.assetmanage.business.inspection.view.already;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inspection.adapter.PatrolListAdapter;
import cn.stronglink.assetmanage.business.inspection.adapter.PatrolOrderAdapter;
import cn.stronglink.assetmanage.business.inspection.model.AlreadyPatrolModel;
import cn.stronglink.assetmanage.business.inspection.model.WaitPatrolModel;
import cn.stronglink.assetmanage.business.inspection.presenter.AlreadyPatrolPresenter;
import cn.stronglink.assetmanage.business.inspection.presenter.WaitPatrolPresenter;
import cn.stronglink.assetmanage.business.inspection.view.wait.WaitPatrolView;
import cn.stronglink.assetmanage.business.inventory.view.IRefreshList;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * 已巡检
 * Created by zhengjinglan on 2018/6/13.
 */

public class AlreadyRepairFragment extends Fragment implements View.OnClickListener,IRefreshList{

    private RecyclerView rv_patrol_order_list;
    private ExpandableListView rv_patrol_list;
    private Button btn_patrol_uncount_qr;
    private Button btn_patrol_uncount_rfid;
    private Button btn_patrol_uncount_stop_rfid;
    private PatrolListAdapter listAdapter;
    private PatrolOrderAdapter orderAdapter;
    private AlreadyPatrolModel alreadyPatrolModel;
    private AlreadyPatrolView alreadyPatrolView;
    private AlreadyPatrolPresenter alreadyPatrolPresenter;
    List<String> groupNameList;
    private LinearLayoutManager layoutManager;
    private TextView txt_title_left,txt_title_right,txt_main_title;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_already_repair_list, container, false);
        try {
            initViewAndData(view);
        }catch (Exception e){
            e.printStackTrace();
        }

        return view;
    }
    private void initViewAndData(View view) {
        txt_title_left = this.getActivity().findViewById(R.id.txt_left_title);
        txt_title_left.setVisibility(View.GONE);
        txt_title_right = this.getActivity().findViewById(R.id.txt_right_title);
        txt_title_right.setVisibility(View.GONE);
        txt_main_title = this.getActivity().findViewById(R.id.txt_main_title);
        txt_main_title.setText("巡检维修");

        rv_patrol_order_list = view.findViewById(R.id.rv_already_order_list);
        rv_patrol_list = view.findViewById(R.id.rv_already_list);
        btn_patrol_uncount_qr = view.findViewById(R.id.btn_already_uncount_qr);
        btn_patrol_uncount_rfid = view.findViewById(R.id.btn_already_uncount_rfid);
        btn_patrol_uncount_stop_rfid = view.findViewById(R.id.btn_already_uncount_stop_rfid);
        layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayout.VERTICAL,false);
        rv_patrol_order_list.setLayoutManager(layoutManager);
        listAdapter = new PatrolListAdapter(getActivity());
        groupNameList = new ArrayList<>();
        groupNameList.add(getResources().getString(R.string.patrol_count));
        orderAdapter = new PatrolOrderAdapter(getActivity(), groupNameList);
        rv_patrol_order_list.setAdapter(listAdapter);
        rv_patrol_list.setAdapter(orderAdapter);
        listAdapter.setOnItemClickListener(new onRecyclerListener() {
            @Override
            public void onItemClicked(View v, int postion) {
                alreadyPatrolPresenter.showOrderByPosition(postion);
            }
        });
        rv_patrol_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                String groupName = groupNameList.get(groupPosition);
                alreadyPatrolPresenter.showDetailByPosition(groupName,childPosition);
                return false;
            }
        });
        btn_patrol_uncount_qr.setOnClickListener(this);
        btn_patrol_uncount_rfid.setOnClickListener(this);
        btn_patrol_uncount_stop_rfid.setOnClickListener(this);
        alreadyPatrolModel = new AlreadyPatrolModel();
        alreadyPatrolView = new AlreadyPatrolView(this, view, listAdapter, orderAdapter);
        alreadyPatrolPresenter = new AlreadyPatrolPresenter(alreadyPatrolModel, alreadyPatrolView);
        this.refreshList();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void refreshList() {
        refreshListHandler.sendEmptyMessage(0);
    }

    private Handler refreshListHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            refreshData();
        }

        private void refreshData() {

            alreadyPatrolPresenter.initInventoryListView();

        }
    };
}
